package com.example.libraryapp.fragment;

import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.example.libraryapp.R;
import com.example.libraryapp.notice.NoticeDetailActivity;
import com.example.libraryapp.notice.NoticeInfo;
import com.example.libraryapp.notice.NoticeService;
import com.example.libraryapp.util.Constance;

/**
 * Fragment
 */
public class ThirdFragment extends Fragment {

	private static Handler handler;
	private Context context;
	private ListView lvNotice;
	private noticeAdapter noticeAdapter;
	private List<NoticeInfo> noticeList;
	private TextView tvTotal;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		init();

	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		if (container == null) {
			return null;
		}
		View view = inflater.inflate(R.layout.third_fragment, container, false);
		findViews(view);
		return view;
	}

	@Override
	public void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		getAllNoticeInfo();
		if (noticeList == null) {
			tvTotal.setText("共计 0  条数据");
		} else {
			tvTotal.setText("共计 " + noticeList.size() + "  条数据");
		}
	}

	/**
	 * 得到所有信息
	 */
	private void getAllNoticeInfo() {
		// 清理数据库
		// LendService.clear(context);
		noticeList = NoticeService.getAllNotice(context);
		noticeAdapter.setData(noticeList);
		noticeAdapter.notifyDataSetChanged();
	}

	/**
	 * 找到控件
	 */
	private void findViews(View view) {
		lvNotice = (ListView) view.findViewById(R.id.lv_notice);
		tvTotal = (TextView) view.findViewById(R.id.tv_total);
		lvNotice.setAdapter(noticeAdapter);
		lvNotice.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				// TODO Auto-generated method stub
				NoticeInfo noticeInfo = noticeList.get(position);
				Intent intent = new Intent(context, NoticeDetailActivity.class);
				intent.putExtra("did", noticeInfo.getId());
				intent.putExtra("title", noticeInfo.getTitle());
				startActivity(intent);
			}
		});
	}

	/**
	 * 初始化方法
	 */
	private void init() {
		// TODO Auto-generated method stub
		context = getActivity();
		noticeAdapter = new noticeAdapter(context);
		handler = new Handler() {

			@Override
			public void handleMessage(Message msg) {
				// TODO Auto-generated method stub
				super.handleMessage(msg);
				switch (msg.what) {
				case Constance.UPDATE_DATA: // 刷新数据
					getAllNoticeInfo();
					break;
				default:
					break;
				}
			}

		};
	}

	public static Handler getHandler() {
		return handler;
	}

	/**
	 * 数据适配器类，给Listview填充数据
	 */
	public class noticeAdapter extends BaseAdapter {
		private List<NoticeInfo> noticeList;
		private LayoutInflater myInflater; // LayoutInfalter对象用来导入布局

		public noticeAdapter(Context context) {
			this.myInflater = LayoutInflater.from(context);
		}

		public void setData(List<NoticeInfo> noticeList) {
			this.noticeList = noticeList;
		}

		public int getCount() {
			if (this.noticeList != null) {
				return this.noticeList.size();
			}
			return 0;
		}

		public Object getItem(int paramInt) {
			return this.noticeList.get(paramInt);
		}

		public long getItemId(int paramInt) {
			return paramInt;
		}

		// 绘制ListItem的方法
		public View getView(final int position, View paramView,
				ViewGroup paramViewGroup) {
			View view = paramView;
			ViewHolder holder = null;

			if (paramView == null) {
				view = myInflater.inflate(R.layout.notice_item, null);
				holder = new ViewHolder();
				holder.tvOrder = (TextView) view.findViewById(R.id.tv_order);
				holder.tvTitle = (TextView) view.findViewById(R.id.tv_title);
				holder.tvPublishingTime = (TextView) view
						.findViewById(R.id.tv_pubtime);
				view.setTag(holder);
			} else {
				holder = (ViewHolder) view.getTag(); // 取出ViewHolder对象
			}
			NoticeInfo notice = this.noticeList.get(position);
			if (notice != null) {
				holder.tvOrder.setText((position + 1) + ".");
				holder.tvTitle.setText(notice.getTitle());
				holder.tvPublishingTime.setText(notice.getPublishingTime());
			}

			return view;

		}

	}

	/**
	 * ListItem界面控件的持有类
	 */
	class ViewHolder {
		TextView tvOrder;
		TextView tvTitle;
		TextView tvPublishingTime;
	}
}
